import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { aggregateData, filterData } from '../utils/dataUtils'

export const useTableStore = defineStore('table', () => {
  // 数据源
  const dataSources = ref([
    {
      id: 'sample_data',
      name: '示例数据',
      type: 'json',
      data: [
        { id: 1, name: '张三', age: 25, department: '技术部', salary: 8000, city: '北京', date: '2024-01-15' },
        { id: 2, name: '李四', age: 30, department: '销售部', salary: 6500, city: '上海', date: '2024-01-16' },
        { id: 3, name: '王五', age: 28, department: '技术部', salary: 9500, city: '深圳', date: '2024-01-17' },
        { id: 4, name: '赵六', age: 35, department: '市场部', salary: 7200, city: '广州', date: '2024-01-18' },
        { id: 5, name: '钱七', age: 26, department: '技术部', salary: 8800, city: '杭州', date: '2024-01-19' },
        { id: 6, name: '孙八', age: 32, department: '销售部', salary: 7000, city: '成都', date: '2024-01-20' },
        { id: 7, name: '周九', age: 29, department: '市场部', salary: 6800, city: '武汉', date: '2024-01-21' },
        { id: 8, name: '吴十', age: 27, department: '技术部', salary: 9200, city: '西安', date: '2024-01-22' }
      ]
    }
  ])

  // 当前选中的数据源
  const currentDataSource = ref(dataSources.value[0])

  // 字段定义
  const fields = ref([
    { id: 'name', name: '姓名', type: 'string', category: '维度', dataType: 'text' },
    { id: 'age', name: '年龄', type: 'number', category: '度量值', dataType: 'number' },
    { id: 'department', name: '部门', type: 'string', category: '维度', dataType: 'text' },
    { id: 'salary', name: '薪资', type: 'number', category: '度量值', dataType: 'currency' },
    { id: 'city', name: '城市', type: 'string', category: '维度', dataType: 'text' },
    { id: 'date', name: '入职日期', type: 'date', category: '维度', dataType: 'date' }
  ])

  // 当前报表配置
  const reportConfig = ref({
    title: '新建报表',
    description: '',
    layout: {
      width: 1200,
      height: 800
    },
    visualizations: []
  })

  // 当前选中的可视化组件
  const selectedVisualization = ref(null)

  // 筛选器配置
  const filters = ref([])

  // 计算属性
  const dimensionFields = computed(() => {
    return fields.value.filter(field => field.category === '维度')
  })

  const measureFields = computed(() => {
    return fields.value.filter(field => field.category === '度量值')
  })

  const currentData = computed(() => {
    if (!currentDataSource.value) return []
    return currentDataSource.value.data
  })

  // 方法
  const addVisualization = (visualization) => {
    const newViz = {
      id: Date.now().toString(),
      type: visualization.type,
      title: visualization.title || `新建${visualization.type}`,
      position: { x: 50, y: 50 },
      size: { width: 400, height: 300 },
      config: visualization.config || {},
      data: visualization.data || []
    }
    reportConfig.value.visualizations.push(newViz)
    return newViz
  }

  const updateVisualization = (id, updates) => {
    const index = reportConfig.value.visualizations.findIndex(viz => viz.id === id)
    if (index !== -1) {
      reportConfig.value.visualizations[index] = {
        ...reportConfig.value.visualizations[index],
        ...updates
      }
    }
  }

  const removeVisualization = (id) => {
    const index = reportConfig.value.visualizations.findIndex(viz => viz.id === id)
    if (index !== -1) {
      reportConfig.value.visualizations.splice(index, 1)
    }
    if (selectedVisualization.value?.id === id) {
      selectedVisualization.value = null
    }
  }

  const selectVisualization = (visualization) => {
    selectedVisualization.value = visualization
  }

  const addFilter = (filter) => {
    filters.value.push({
      id: Date.now().toString(),
      field: filter.field,
      type: filter.type,
      value: filter.value,
      operator: filter.operator || 'equals'
    })
  }

  const removeFilter = (id) => {
    const index = filters.value.findIndex(filter => filter.id === id)
    if (index !== -1) {
      filters.value.splice(index, 1)
    }
  }

  const getFilteredData = () => {
    return filterData(currentData.value, filters.value)
  }

  const aggregateDataLocal = (data, groupBy, measures) => {
    return aggregateData(data, groupBy, measures)
  }

  return {
    // 状态
    dataSources,
    currentDataSource,
    fields,
    reportConfig,
    selectedVisualization,
    filters,
    
    // 计算属性
    dimensionFields,
    measureFields,
    currentData,
    
    // 方法
    addVisualization,
    updateVisualization,
    removeVisualization,
    selectVisualization,
    addFilter,
    removeFilter,
    getFilteredData,
    aggregateData: aggregateDataLocal
  }
})